#!/bin/sh
#

read -p "输入项目代码：msydloan；crcs；msreport；qdzjmsloan>" project
path="/data/project/"${project}
if [ "$project" == "msydloan" ];then 
	pro_name=${path}"/ydloan-web"
elif [ "$project" == "crcs" ];then
        pro_name=${path}"/crcs-web"
elif [ "$project" == "msreport" ];then
        pro_name=${path}"/report-web"
else
        pro_name="输入的服务无效，自动退出！"
        exit 0
fi


function change_branch(){
	echo $path
	cd $path
	git pull
	git branch -a
	read -p "输入切换分支的命令，不需要切换直接Enter键：>" order
	if [ "$order" != "" ];then
		git checkout $order
		    sleep 2
        	git pull
        	sleep 2
        	echo "切换分支成功...."
	else
	        echo "最新分支，不需要切换分支"
	fi
}


function start_process() {
	str=$(ps aux| grep $path | grep -v 'grep'|awk '{print $2}')
        echo $str
        if [ "$str" != "" ];then
                read -p "输入要杀掉的进程，多个进程时空格隔开>" process
                kill $process
                echo "等待10秒后判断是否已经关闭进程"
                sleep 10
                str1=$(ps aux| grep $path | grep -v 'grep'|awk '{print $2}')
                if [ "$str1" == "" ];then
                        echo "已经关闭了进程，开始拉代码编译....."
                        git pull
                        echo "拉取最新代码完成！"
                        mvn clean install -P stage
                        sleep 2
                        echo "编译后等待2秒后启动服务....."
		        cd $pro_name
			echo "切换到的服务目录为："$pro_name
                        nohup mvn spring-boot:run -P stage &
                        sleep 2
                        echo "启动服务后等待2秒后，查看启动状态"
                        tail -f nohup.out
                else
                        echo "进程还未关闭，再次重启shell脚本"
                fi
        else
                echo "进程是关闭状态，开始拉代码编译....."
                git pull
                echo "拉取最新代码完成！"
                mvn clean install -P stage
                sleep 2
                echo "编译后等待2秒后启动服务....."
                cd $pro_name
		echo "切换到的服务目录为："$pro_name
                nohup mvn spring-boot:run -P stage &
                sleep 2
                echo "启动服务后等待2秒后，查看启动状态"
                tail -f nohup.out
        fi
}


function show_logs() {
	read -p "输入要查看的服务>" server
	server_path=$path"/"$server
	str1=$(ps aux| grep $server_path | grep -v 'grep'|awk '{print $2}')
        echo $str1
        cd $server_path
        echo "切换到服务启动目录"$server_path"....."
        if [ "$str1" == "" ];then
                echo "进程未开启，开始启动服务......"
                nohup mvn spring-boot:run -P stage &
                sleep 2
                echo "启动服务后等待2秒后，查看启动日志"
                tail -f nohup.out
        else 
		read -p "服务已经是启动状态，需要重启服务输入需要杀掉的进程，否则直接enter> " process
		if [ "$process" != "" ];then
               		 kill $process
               		 echo "等待5秒后启动服务进程"
               		 sleep 5
			 nohup mvn spring-boot:run -P stage &
               		 sleep 2
               		 echo "启动服务后等待2秒后，查看启动状态"
               		 tail -f nohup.out
		else
			echo "直接查看日志...."
			tail -f nohup.out
		fi
        fi
}


if [ $1 == "start" ];then
        change_branch
        start_process

elif [ $1 == "log" ];then
        show_logs

elif [ $1 == "" ];then
        echo "请输入一个参数：start-切换版本、拉代码、启动web服务；log-启动具体服务查看日志"
        exit 0
else
        echo "输入的参数错误,退出shell脚本！"
        exit 0
fi
